package web.weibo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import web.weibo.model.User;
import web.weibo.model.UserRole;
import web.weibo.service.ServiceExample;

import java.util.ArrayList;

@RestController
public class ControllerExample {

    ServiceExample service;

    public ControllerExample(ServiceExample service) {
        this.service = service;
    }

    @GetMapping("/example/transaction")
    public ModelAndView transactionExample() {
        String message = service.TtransactionExample();
        ModelAndView modelAndView = new ModelAndView("demo");
        modelAndView.addObject("message", message);
        return modelAndView;
    }

    @GetMapping("/example/redis")
    public ModelAndView redisExample() {
        String message = service.redisExample();
        ModelAndView modelAndView = new ModelAndView("demo");
        modelAndView.addObject("message", message);
        return modelAndView;
    }

    @GetMapping("/example/cache")
    public ModelAndView cacheExample() {
        String message = service.cacheExample();
        ModelAndView modelAndView = new ModelAndView("demo");
        modelAndView.addObject("message", message);
        return modelAndView;
    }

    @RequestMapping("/example/json")
    public ArrayList<User> jsonExample() {
        User user1 = new User(1, "test1", "test1", "test1", UserRole.normal, "test1", "xx.jpg");
        User user2 = new User(2, "test2", "test2", "test2", UserRole.normal, "test2", "xx.jpg");
        ArrayList<User> user = new ArrayList<>();
        user.add(user1);
        user.add(user2);
        return user;
    }
}
